package com.suxin.test;

import com.suxin.domain.User;
import com.suxin.job.MyJobListener;
import com.suxin.job.MyTriggerListener;
import com.suxin.job.TestJob;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.KeyMatcher;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.core.env.MapPropertySource;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * @author Tang
 * @classname Main
 * @description [  ]
 * @date 2021/3/12 16:56
 */
public class Main {

    public static void main(String[] args) throws Exception {

        User user = new User();
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(user);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("name", "tang");
        propertyValues.add("age", 18);
        propertyValues.add("sex", "1");
        beanWrapper.setPropertyValues(propertyValues,true);

        System.out.println(user);
    }

    /**
     * cron 表达式
     * @throws Exception
     */
    @Test
    public void test02() throws Exception {
        Scheduler defaultScheduler = StdSchedulerFactory.getDefaultScheduler();
        defaultScheduler.start();
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("testJob", "testJobGroup")
                .build();
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("testJob", "testJobGroup")
                // 携带一些map数据
                .usingJobData("name", "tang")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? *"))
                .build();
        defaultScheduler.scheduleJob(jobDetail, trigger);
        TimeUnit.SECONDS.sleep(60);
        defaultScheduler.shutdown();
    }

    /**
     * 定时版
     * @throws Exception
     */
    @Test
    public void test01() throws Exception{
        // 创建调度器 Scheduler
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
        // 创建 JobDetail 实例
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("job1", "group1")
                .build();
        // 创建 Trigger 实例
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(simpleSchedule()
                        // 每隔10秒执行一次
                        .withIntervalInSeconds(10)
                        // 一直执行
                        .repeatForever()
                ).build();
        ListenerManager listenerManager = scheduler.getListenerManager();
        listenerManager.addJobListener(
                new MyJobListener(), KeyMatcher.keyEquals(JobKey.jobKey("job1","group1"))
        );
        listenerManager.addTriggerListener(
                new MyTriggerListener(),KeyMatcher.keyEquals(TriggerKey.triggerKey("trigger1","group1"))
        );
        // 调度器调度执行
        scheduler.scheduleJob(jobDetail, trigger);
        TimeUnit.SECONDS.sleep(60);
        scheduler.shutdown();
    }

}